package com.coderlin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.coderlin.entities.Customer;
import com.coderlin.entities.History;
import com.coderlin.entities.Worker;
import com.coderlin.mapper.HistoryMapper;
import com.coderlin.service.HistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * <Description> <br>
 *
 * @author Coder_Lin<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2021/07/24 10:01 <br>
 * @see com.coderlin.service.impl <br>
 */
@Service
@Slf4j
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;
    /**
     * 联表查询列出所有的history记录
     *
     * @return
     */
    @Override
    public List<History> listHistory() {
        return historyMapper.listHistory();
    }

    /**
     * 添加history记录
     *
     * @param history
     * @return
     */
    @Override
    public Integer addHistory(History history ,Integer cid, Integer wid) {
        Customer customer = new Customer();
        Worker worker = new Worker();
        customer.setCid(cid);
        worker.setWid(wid);
        history.setCustomer(customer);
        history.setWorker(worker);

        return historyMapper.addHistory(history);
    }

    /**
     * 根据ID删除送水记录
     *
     * @param hid
     * @return
     */
    @Override
    public Integer delHistoryById(int hid) {
        return historyMapper.delHistoryById(hid);
    }

    /**
     * 根据ID查找历史记录
     *
     * @param hid
     * @return
     */
    @Override
    public History listHistoryById(int hid) {
        return historyMapper.listHistoryById(hid);
    }

    /**
     * 根据传入的数据修改数据库内容
     *
     * @param history
     * @param cid
     * @param wid
     * @return
     */
    @Override
    public Integer modHistory(History history, Integer cid, Integer wid) {
        Customer customer = new Customer();
        Worker worker = new Worker();
        customer.setCid(cid);
        worker.setWid(wid);
        history.setCustomer(customer);
        history.setWorker(worker);

        return historyMapper.modHistory(history);
    }

    /**
     * 根据时间区间查找送水历史
     *
     * @param sendWaterTimeStart
     * @param sendWaterTimeEnd
     * @return
     */
    @Override
    public List<History> searchHistoryByTimeRange(String sendWaterTimeStart, String sendWaterTimeEnd) throws ParseException {
        return historyMapper.searchHistoryByTimeRange(sendWaterTimeStart,sendWaterTimeEnd);
    }

    /**
     * 将字符串转化为List,删除id列表中的id数据
     *
     * @param idList
     * @return
     */
    @Transactional(rollbackFor = {Exception.class,Error.class})
    @Override
    public Integer batchDeleteHistoryById(String idList) {
        idList=idList.replaceFirst(",","");
        String[] split = StrUtil.split(idList, ",");
        List<Integer> integers = new ArrayList<>();
        for (String s : split) {
            integers.add(Integer.parseInt(s));
        }
        if(log.isInfoEnabled()){
            log.info("historyService batchDelete"+String.valueOf(integers));
        }
//        int i = 1 / 0;
        return historyMapper.batchDeleteHistoryById(integers);
    }

}
